<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.bootcdn.net/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
</head>

<body>

</body>
<script>
    var formData = {
        username: '',//用户名
        nickname: 'tom',//昵称
        age: undefined,//年龄
        hobby: null,//兴趣
        sex: 0,//性别,0-女, 1-男
    }
    /*
    var filterFormData = {}
    for (const key in formData) {
        if (formData[key] || formData[key] === 0) {
            filterFormData[key] = formData[key]
        }
    }

    console.log(filterFormData)
    */
    //优化思路1-es6里面有没有好的方法
    //优化思路2-lodash里面有没有好的方法(推荐)
    //_.omitBy忽略
    // var filterFormData = _.omitBy(formData, )
    // var filterFormData = _.omitBy(formData, (value, key)=>_.isEmpty(value))
    var filterFormData = _.omitBy(formData, (value, key) => _.isEmpty(value) && !_.isNumber(value))
    console.log(filterFormData)
</script>

</html>